/*
* formula 公式
*
* 此文件中抽象了一些计算逻辑
*
* */

import {
    isVoid,
    isNotVoid,
    isVoidArg,
    isNotVoidArg,
    calcAnnualizedByTerminationPaymentDate,
    isEquativesABC,
    isEquativesAnnualizedABC,
    isEqualNum,
    mulArg,
    round,
} from '@/utils/util'
import _ from 'lodash'

/**
 * 价格转换比例
 * @param price
 * @param baseNum 名义本金或期初价格
 * @param another 年化系数
 * @constructor
 */
export function PriceConvertPercentage(price,baseNum,another){
    if (isVoid(price)) {
        return  0
    }
    if (isNotVoid(baseNum)) {
        let result = _.multiply(_.divide(price, baseNum), 100)
        if (Boolean(another)) {
            return   _.divide(result,another)
        }

        return   _.round(result,2)
    }
}

/**
 * 比例转换价格
 * @param percentage
 * @param baseNum 名义本金或期初价格
 * @param another 年化系数
 * @returns {number|*}
 * @constructor
 */
export function PercentageConvertPrice(percentage,baseNum,another){
    if (isVoid(percentage)) {
        return  0
    }

    if (isNotVoid(baseNum)) {
        let result = _.multiply(_.divide(percentage,100), baseNum)
        if (Boolean(another)) {
            return   _.multiply(result,another)
        }
        return   _.round(result,2)
    }
}

/**
 * 判断两个数字是否以满足逻辑 price = baseNum * percentage [* another]
 * @param baseNum
 * @param Percentage
 * @param Price
 * @param another 可选
 * @returns {boolean}
 * @constructor
 */
function LinkValid(baseNum, Percentage, Price,another) {
    if (isNotVoid(String(baseNum)) && isNotVoid(String(Percentage)) && isNotVoid(String(Price))) {
        if (isNotVoid(another)){
            return _.isEqual(Price, _.multiply(_.divide(Percentage, 100), _.multiply(baseNum,another)).toFixed(4))
        }else {
            return _.isEqual(Price, _.multiply(_.divide(Percentage, 100), baseNum).toFixed(4))
        }

    }
}
/**
 * var object = { 'a': [{ 'b': { 'c': 3 } }] };
 *
 _.set(object, 'a[0].b.c', 4);
 console.log(object.a[0].b.c);
 // => 4

 _.set(object, ['x', '0', 'y', 'z'], 5);
 console.log(object.x[0].y.z);
 // => 5
 * @param object 要修改的对象。
 * @param path 要设置的对象路径。
 * @param newValue 要设置的值。
 */
export function setValueAppoint(object,path,newValue){
    _.set(object, path,newValue)
}

export function  linkPercentage(obj,divisor,baseNum,path){
    if (obj.data.priceOnWait == 0){
        linkPercentageSource(obj,divisor,baseNum,path)
    }
}

export function  linkPrice(obj,divisor,baseNum,path){
    if (obj.data.priceOnWait == 0){
        linkPriceSource(obj,divisor,baseNum,path)
    }
}

export function  linkPercentageSource(obj,divisor,baseNum,path){
        let newValue = PercentageConvertPrice(divisor,baseNum);
        setValueAppoint(obj,path,newValue)
}

export function  linkPriceSource(obj,divisor,baseNum,path){
        let newValue = PriceConvertPercentage(divisor,baseNum);
        setValueAppoint(obj,path,newValue)
}